package indi.tshoiasc.simstr.utils;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class PlayerOperate {
	public static boolean hasItem(Player p, List<ItemStack> im) {
		boolean flag = true;
		for (int i = 0; i < im.size(); i++) {
			ItemStack it = (ItemStack) im.get(i);
			HashMap<Integer, ? extends ItemStack> itemlist = p.getInventory().all(((ItemStack) im.get(i)).getType());
			Collection<? extends ItemStack> value = itemlist.values();
			int result = 0;
			for (ItemStack items : value) {
				if ((items.getItemMeta().equals(it.getItemMeta())) || ((!items.hasItemMeta()) && (!it.hasItemMeta()))) {
					result += items.getAmount();
				}
			}
			if (result < it.getAmount()) {
				flag = false;
				break;
			}
		}
		return flag;
	}

	public static boolean takeItem(Player p, List<ItemStack> im, boolean has) {
		if ((!has) && (!hasItem(p, im))) {
			return false;
		}
		for (ItemStack it : im) {
			p.getInventory().removeItem(new ItemStack[] { it });
		}
		return true;
	}
}
